package com.zlc.common.util;


import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * 日期处理
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils{
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";


    private static final String[] PARSE_PATTERNS = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};


    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static String parse(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }


    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), PARSE_PATTERNS);
        } catch (ParseException e) {
            return null;
        }
    }


    public static List<Date> getTwoDateMonths(Date startDate, Date endDate) {
        List<Date> dates = new ArrayList<>();

        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            // 获取开始年份和开始月份
            int startYear = calendar.get(Calendar.YEAR);
            int startMonth = calendar.get(Calendar.MONTH);
            // 获取结束年份和结束月份
            calendar.setTime(endDate);
            int endYear = calendar.get(Calendar.YEAR);
            int endMonth = calendar.get(Calendar.MONTH);
            //
            List<String> list = new ArrayList<String>();
            for (int i = startYear; i <= endYear; i++) {
                String date = "";
                if (startYear == endYear) {
                    for (int j = startMonth; j <= endMonth; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }

                } else {
                    if (i == startYear) {
                        for (int j = startMonth; j < 12; j++) {
                            if (j < 9) {
                                date = i + "-0" + (j + 1);
                            } else {
                                date = i + "-" + (j + 1);
                            }
                            list.add(date);
                        }
                    } else if (i == endYear) {
                        for (int j = 0; j <= endMonth; j++) {
                            if (j < 9) {
                                date = i + "-0" + (j + 1);
                            } else {
                                date = i + "-" + (j + 1);
                            }
                            list.add(date);
                        }
                    } else {
                        for (int j = 0; j < 12; j++) {
                            if (j < 9) {
                                date = i + "-0" + (j + 1);
                            } else {
                                date = i + "-" + (j + 1);
                            }
                            list.add(date);
                        }
                    }

                }

            }

            // 所有的月份已经准备好
            //System.out.println(list);
            for (String str : list) {
                dates.add(strToDate(str, "yyyy-MM"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return dates;
    }

    public static String getNowMonthDate() {
        return format(new Date(), "MM-dd");
    }


    public static String getNowMonth() {
        return format(new Date(), "yyyy-MM");
    }


    public static String getMonthDateStr(String dateStr) {
        Date date = strToDate(dateStr);
        return format(date, "yyyy-MM");
    }


    public static String dateToYM(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        return formatter.format(dateDate);
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间格式 yyyy-MM-dd
     */
    public static Date getNowDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        return formatter.parse(dateString, pos);
    }

    /**
     * 获取现在时间
     *
     * @return 返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(currentTime);
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(currentTime);
    }

    /**
     * 获取时间 小时:分;秒 HH:mm:ss
     *
     */
    public static String getTimeShort() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date currentTime = new Date();
        return formatter.format(currentTime);
    }

    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(strDate, pos);
    }

    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
     *
     */
    public static String dateToStrLong(Date dateDate) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter.format(dateDate);
        }catch (Exception e){
            return "";
        }
    }

    /**
     * 将长时间格式时间转换为字符串 yyyy年MM月dd日 HH时mm分
     *
     */
    public static String dateToString(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
        return formatter.format(dateDate);
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     */
    public static String dateToStr(Date dateDate) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            return formatter.format(dateDate);
        }catch (Exception e){
            return "";
        }
    }

    public static String dateToStrMoneth(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        return formatter.format(dateDate);
    }

    public static String dateToStr(String dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = null;
        try {
            dateString = formatter.format(formatter.parse(dateDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateString;
    }


    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(strDate, pos);
    }


    public static Date strToDate(String strDate, String pattern) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            ParsePosition pos = new ParsePosition(0);
            return formatter.parse(strDate, pos);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 得到现在时间
     *
     */
    public static Date getNow() {
        return new Date();
    }

    /**
     * 提取一个月中的最后一天
     *
     */
    public static Date getLastDate(long day) {
        Date date = new Date();
        long date_3_hm = date.getTime() - 3600000 * 24 * day;
        return new Date(date_3_hm);
    }


    /**
     * 得到现在时间
     *
     * @return 字符串 yyyyMMdd HHmmss
     */
    public static String getStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
        return formatter.format(currentTime);
    }

    /**
     * 得到现在时间
     *
     * @return 字符串 yyyyMMdd
     */
    public static String getStringToday2() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        return formatter.format(currentTime);
    }


    /**
     * 得到现在时间
     *
     * @return 字符串 yyyyMMdd HHmmss
     */
    public static String getStringTodayForFileName() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss");
        return formatter.format(currentTime);
    }

    /**
     * 得到现在小时
     */
    public static String getHour() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String hour;
        hour = dateString.substring(11, 13);
        return hour;
    }



    /**
     * 得到现在分钟
     *
     */
    public static String getTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String min;
        min = dateString.substring(14, 16);
        return min;
    }

    /**
     * 根据用户传入的时间表示格式，返回当前时间的格式 如果是yyyyMMdd，注意字母y不能大写。
     *
     */
    public static String getUserDate(String sformat) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(sformat);
        return formatter.format(currentTime);
    }

    /**
     * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式，返回字符型的分钟
     */
    public static String getTwoHour(String st1, String st2) {
        String[] kk = null;
        String[] jj = null;
        kk = st1.split(":");
        jj = st2.split(":");
        if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
            return "0";
        else {
            double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
            double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
            if ((y - u) > 0)
                return y - u + "";
            else
                return "0";
        }
    }

    /**
     * 得到二个日期间的间隔天数
     */
    public static String getTwoDay(String sj1, String sj2) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        long day = 0;
        try {
            Date date = myFormatter.parse(sj1);
            Date mydate = myFormatter.parse(sj2);
            day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (Exception e) {
            return "";
        }
        return day + "";
    }

    /**
     * 时间前推或后推分钟,其中JJ表示分钟.
     */
    public static String getPreTime(String sj1, String jj) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String mydate1 = "";
        try {
            Date date1 = format.parse(sj1);
            long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60L;
            date1.setTime(Time * 1000);
            mydate1 = format.format(date1);
        } catch (Exception e) {
        }
        return mydate1;
    }

    /**
     * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
     */
    public static String getNextDay(Date nowdate, Integer delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String mdate = "";
            Date d = new Date(nowdate.getTime());
            long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
            d.setTime(myTime * 1000);
            mdate = format.format(d);
            return mdate;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
     */
    public static Date getNextSecond(Date nowdate, Integer delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String mdate = "";
            Date d = new Date(nowdate.getTime());
            long myTime = (d.getTime() / 1000) + delay;
            return new Date(myTime * 1000);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
     */
    public static Date getNextMinute(Date nowdate, Integer delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = new Date(nowdate.getTime());
            long myTime = (d.getTime() / 1000) + delay * 60;
            return new Date(myTime * 1000);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getNextDataDay(Date nowdate, Integer delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String mdate = "";
            Date d = new Date(nowdate.getTime());
            long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
            d.setTime(myTime * 1000);
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
     */
    public static String getNextDay1(Date nowdate, Integer delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            String mdate = "";
            Date d = new Date(nowdate.getTime());
            long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
            d.setTime(myTime * 1000);
            mdate = format.format(d);
            return mdate;
        } catch (Exception e) {
            return "";
        }
    }


    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     */
    public static int daysBetween(Date smdate, Date bdate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);

            return Integer.parseInt(String.valueOf(between_days));
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 　　 *字符串的日期格式的计算
     *
     */
    public static int daysBetween(String smdate, String bdate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);

            return Integer.parseInt(String.valueOf(between_days));
        }catch (Exception e){

        }
        return 0;
    }


    public static Date getNextDay1(Date nowdate) {
        try {
            Date d = new Date(nowdate.getTime());
            long myTime = (d.getTime() / 1000) + 24 * 60 * 60L;
            d.setTime(myTime * 1000);
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getLastMonthStart(Date date) {
        Date start = getMonthStart(date);
        Date month = new Date(start.getTime() - (10 * 24 * 60 * 60 * 1000));
        return DateUtils.getMonthStart(month);
    }


    /**
     * 判断是否润年
     *
     */
    public static boolean isLeapYear(String ddate) {

        /**
         * 详细设计： 1.被400整除是闰年，否则： 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
         * 3.能被4整除同时能被100整除则不是闰年
         */
        Date d = strToDate(ddate);
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(d);
        int year = gc.get(Calendar.YEAR);
        if ((year % 400) == 0) {
            return true;
        } else if ((year % 4) == 0) {
            if ((year % 100) == 0) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 返回美国时间格式 26 Apr 2006
     *
     */
    public static String getEDate(String str) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(str, pos);
        String j = strtodate.toString();
        String[] k = j.split(" ");
        return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
    }

    /**
     * 获取一个月的最后一天
     * @param dat yyyy-MM-dd
     *
     */
    public static String getEndDateOfMonth(String dat) {
        String str = dat.substring(0, 8);
        String month = dat.substring(5, 7);
        int mon = Integer.parseInt(month);
        if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
            str += "31";
        } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
            str += "30";
        } else {
            if (isLeapYear(dat)) {
                str += "29";
            } else {
                str += "28";
            }
        }
        return str;
    }
    

    /**
     * 判断二个时间是否在同一个周
     *
     */
    public static boolean isSameWeekDates(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (0 == subYear) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if (1 == subYear && Calendar.DECEMBER == cal2.get(Calendar.MONTH)) {
            // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if (-1 == subYear && Calendar.DECEMBER == cal1.get(Calendar.MONTH)) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 产生周序列,即得到当前时间所在的年度是第几周
     *
     */
    public static String getSeqWeek() {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
        if (week.length() == 1)
            week = "0" + week;
        String year = Integer.toString(c.get(Calendar.YEAR));
        return year + week;
    }


    /**
     * 取得当前日期所在周的第一天
     *
     */
    public static Date getFirstDayOfWeek() {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     */
    public static Date getLastDayOfWeek() {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     * 根据一个日期，返回是星期几的字符串
     *
     */
    public static String getWeek(String sdate) {
        // 再转换为时间
        Date date = strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // int hour=c.get(Calendar.DAY_OF_WEEK);
        // hour中存的就是星期几了，其范围 1~7
        // 1=星期日 7=星期六，其他类推
        return new SimpleDateFormat("EEEE").format(c.getTime());
    }

    public static String getWeekStr(String sdate) {
        Date date = strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Integer i = c.get(Calendar.DAY_OF_WEEK);
        String str = "";
        str = getWeek(sdate);
        if (i.equals(1)) {
            str = "星期日";
        } else if (i.equals(2)) {
            str = "星期一";
        } else if (i.equals(3)) {
            str = "星期二";
        } else if (i.equals(4)) {
            str = "星期三";
        } else if (i.equals(5)) {
            str = "星期四";
        } else if (i.equals(6)) {
            str = "星期五";
        } else if (i.equals(7)) {
            str = "星期六";
        }
        return str;
    }


    public static Date addDayToTime(Date date,Integer day,String time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,day);

        String s = dateToStr(calendar.getTime());

        return strToDateLong(s +" " + time);
    }

    /**
     * 给一个日期时间加上多少天
     */
    public static Date addDayToTimeV2(Date date,Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,day);

        String s = dateToStrLong(calendar.getTime());

        return strToDateLong(s);
    }


    /**
     * 两个时间之间的天数
     *
     */
    public static long getDays(String date1, String date2) {
        if (date1 == null || date1.equals(""))
            return 0;
        if (date2 == null || date2.equals(""))
            return 0;
        // 转换为标准时间
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date mydate = null;
        try {
            date = myFormatter.parse(date1);
            mydate = myFormatter.parse(date2);
        } catch (Exception e) {
        }
        return (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取两个时间之间的毫秒数
     *
     */
    public static long getDayTimes(Date date1, Date date2) {
        if (date1 == null || date2 == null) return 0;

        return date1.getTime() - date2.getTime();
    }

    /**
     * 获取两个时间之间的小时数
     *
     */
    public static long getDayHours(Date date1, Date date2) {
        if (date1 == null || date2 == null) return 0;

        return (date1.getTime() - date2.getTime()) / (60 * 60 * 1000);
    }

    public static Integer getDayHours(Date date1) {
        if (date1 == null) return 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }


    /**
     * 获取两个时间段内的所有周日的天数
     *
     */
    public static int getSundayCount(String from, String to) {
        Calendar calendar = Calendar.getInstance();
        String[] array = {from, to};
        Date[] ds = new Date[array.length];

        for (int i = 0; i < array.length; i++) {
            String[] fs = array[i].split("[^\\d]+");
            calendar.set(Integer.parseInt(fs[0]), Integer.parseInt(fs[1]) - 1, Integer.parseInt(fs[2]));
            ds[i] = calendar.getTime();
        }
        int count = 0;
        for (Date x = ds[0]; x.compareTo(ds[1]) < 0; ) {
            calendar.setTime(x);
            calendar.add(Calendar.DATE, 1);
            x = calendar.getTime();
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                count++;
            }
        }
        return count;
    }

    /**
     * 获取year年month月的最后一天的最后一个小时最后一个分钟的最后一秒
     *
     */
    public static Date getLastDay(int year, int month) {
        Calendar c = Calendar.getInstance();
        //c.clear();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, lastDay);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime();
    }

    /**
     * 获取year年month月的第一天的第一个小时第一个分钟的第一秒
     *
     */
    public static Date getFirstDay(int year, int month) {
        Calendar c = Calendar.getInstance();
        //c.clear();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    /**
     * 获取某个月有多少天
     *
     */
    public static int getDaysOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getActualMaximum(Calendar.DATE);
    }

    /**
     * 获取一年的所有天数
     *
     */
    public static List<String> getAllDays(int year) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        List<String> dates = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            Calendar cal = Calendar.getInstance();// 获得当前日期对象
            cal.clear();// 清除信息
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, i);// 1月从0开始
            for (int j = 1; j <= cal.getActualMaximum(Calendar.DAY_OF_MONTH); j++) {
                cal.set(Calendar.DAY_OF_MONTH, j);
                dates.add(df.format(cal.getTime()));
            }
        }
        return dates;
    }

    public static String getUTCTimeStr() {
        Calendar cal = Calendar.getInstance();
        return String.valueOf(cal.getTimeInMillis() / 1000);// 返回的就是UTC时间
    }

    /**
     * 上个月的最后一天
     *
     */
    public static String getTheLastMonthEnd() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);    //按格式输出
        return sdf.format(c.getTime());
    }

    /**
     * 上个月的第一天
     *
     */
    public static String getTheLastMonthStart() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 00, 00, 00);    //按格式输出
        return sdf.format(c.getTime());
    }

    /**
     * 上个月的最后一天
     *
     */
    public static Date getTheLastMonthEndDate() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);    //按格式输出
        return c.getTime();
    }

    /**
     * 上个月的第一天
     *
     */
    public static Date getTheLastMonthStartDate() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 00, 00, 00);    //按格式输出
        return c.getTime();
    }

    /**
     * 本月的最后一天
     *
     */
    public static Date getMonthEnd() {
        Calendar c = Calendar.getInstance();
        int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);    //按格式输出
        return c.getTime();
    }

    /**
     * 本月的第一天
     *
     */
    public static Date getMonthStart() {
        Calendar c = Calendar.getInstance();
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 00, 00, 00);    //按格式输出
        return c.getTime();
    }

    public static Date getMonthStart(Date time) {
        String monthStr = parse(time, "yyyy-MM");
        return strToDate(monthStr + "-01");
    }

    public static String getMonthStartStr(Date time) {
        String monthStr = parse(time, "yyyy-MM");
        return monthStr + "-01";
    }

    public static Date getMonthEnd(Date end) {
        String monthStr = parse(end, "yyyy-MM-dd");
        String endStr = getEndDateOfMonth(monthStr);
        endStr = endStr + " 23:59:59";
        return strToDate(endStr, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getMonthEndStr(Date end) {
        String monthStr = parse(end, "yyyy-MM-dd");
        return getEndDateOfMonth(monthStr) + " 23:59:59";
    }

    public static Date getYearStart(Date time) {
        String yearStr = parse(time, "yyyy");
        return strToDate(yearStr + "-01-01");
    }

    public static Date getYearEnd(Date time) {
        String yearStr = parse(time, "yyyy");
        return strToDate(yearStr + "-12-31 23:59:59");
    }


    /**
     * 本月的最后一天
     *
     */
    public static String getMonthEndStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Calendar c = Calendar.getInstance();
        int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);    //按格式输出
        return sdf.format(c.getTime());
    }

    /**
     * 本月的第一天
     *
     */
    public static String getMonthStartStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 00, 00, 00);    //按格式输出
        return sdf.format(c.getTime());
    }


    /**
     * 获取今年最后一天的最后一个小时最后一个分钟的最后一秒
     *
     */
    public static String getLastDayThisYear() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 31);
        c.set(Calendar.MONTH, 11);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        return sdf.format(c.getTime());
    }

    /**
     * 获取year年month月的第一天的第一个小时第一个分钟的第一秒
     *
     */
    public static String getFirstDayThisYear() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        return sdf.format(c.getTime());
    }

    public static String getMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("M");
        return sdf.format(c.getTime());
    }

    public static String getMonthStr() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("M");
        String value =  sdf.format(c.getTime());
        if(value.length() == 1){
            return "0"+value;
        }
        return value;
    }


    public static String getYearAndMonth(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(time);
    }

    /**
     * 获取指定月第一天
     * 返回示例：Date类型
     * 传入格式：2019-09
     */
    public static Date getFrastDayDate(String month) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse((month.trim() + "-01 00:00:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定月第一天
     * 返回示例：2019-1-1 00:00:00
     * 传入格式：2019-09
     *
     */
    public static String getFrastDay(String month) {
        return month.trim() + "-01 00:00:00";
    }

    /**
     * 获取指定月最后一天
     * 返回示例：2019-1-31 23:59:59
     * 传入格式：2019-09
     *
     */
    public static String getLastDay(String month) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = format.parse(month.trim() + "-01");
            Calendar ca = Calendar.getInstance();
            ca.setTime(parse);
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String day2 = format.format(ca.getTime());
            return day2 + " 23:59:59";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当月第一天
     * 返回示例：2019-1-1 00:00:00
     */
    public static String getFrastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//1:本月第一天
        String day1 = format.format(c.getTime());
        return day1 + " 00:00:00";
    }

    public static String getFrastDay(Date time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(time);

        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//1:本月第一天
        String day1 = format.format(c.getTime());
        return day1 + " 00:00:00";
    }

    /**
     * 获取当月最后一天
     * 返回示例：2019-1-31 23:59:59
     */
    public static String getLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String day2 = format.format(ca.getTime());
        return day2 + " 23:59:59";
    }

    public static String getLastDay(Date time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.setTime(time);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String day2 = format.format(ca.getTime());
        return day2 + " 23:59:59";
    }


    public static Date getLastDay(Date time,Integer delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date d = new Date(time.getTime());
            long myTime = (d.getTime() / 1000) - (delay * 24 * 60 * 60);
            d.setTime(myTime * 1000);
            return format.parse(format.format(d));
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 获取几后的今天
     */
    public static Date getNextYearNow(int i) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(getNow());
        ca.add(Calendar.YEAR, i);
        return ca.getTime();
    }

    /**
     * @return
     */
    public static Date getNextYearOfDate(Date date, int i) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.YEAR, i);
        return ca.getTime();
    }

    public static Date getNextMonthOfDate(Date date,int i) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.MONTH, i);
        return ca.getTime();
    }

    public static void main(String[] args) {
        String endDateOfMonth = getEndDateOfMonth("2022-03-01");
        System.out.println();
    }

    public static String getYear() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR) + "";
    }


    /**
     * 获取本月
     */
    public static String getStringMonth() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        return formatter.format(currentTime);
    }

    public static String getStringMonth(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        return formatter.format(date);
    }


    /**
     * 获取下月的今天
     */
    public static Date getNextDay() {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取下月的月份
     * 返回示例 2019-09
     */
    public static String getNextMonthString() {
        Date nextDay = getNextDay();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        return simpleDateFormat.format(nextDay);
    }

    public static Integer getBetweenMonthNumber(Date start, Date end) {
        Long endTime = end.getTime();
        Long startTime = start.getTime();
        Long betweenTime = endTime - startTime;
        return BigDecimal.valueOf(betweenTime).divide(BigDecimal.valueOf(31 * 24 * 60 * 60 * 1000L), 0, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static Date getDayEnd(Date day) {
        Date date = strToDate(format(day, "yyyy-MM-dd"));
        return new Date(getNextDataDay(date, 1).getTime() - 1);
    }

    public static Date getDayStart(Date day) {
        return strToDate(format(day, "yyyy-MM-dd"));
    }


    public static List<Date> getBetweenMonth(Date start, Date end){
        List<Date> dates = new ArrayList<>();
        while(start.before(end)){
            dates.add(DateUtils.getMonthStart(start));
            start = DateUtils.getNextMonthOfDate(start,1);
        }
        return dates;
    }

    public static Integer getBetweenDayNumber(Date start, Date end) {
        Long endTime = end.getTime();
        Long startTime = start.getTime();
        Long betweenTime = endTime - startTime;
        return BigDecimal.valueOf(betweenTime).divide(BigDecimal.valueOf(24 * 60 * 60 * 1000L), 0, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static Integer getBetweenHourNumber(Date start, Date end) {
        Long endTime = end.getTime();
        Long startTime = start.getTime();
        Long betweenTime = endTime - startTime;
        return BigDecimal.valueOf(betweenTime).divide(BigDecimal.valueOf(60 * 60 * 1000L), 0, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static Integer getBetweenSecondNumber(Date start, Date end) {
        Long endTime = end.getTime();
        Long startTime = start.getTime();
        Long betweenTime = endTime - startTime;
        return BigDecimal.valueOf(betweenTime).divide(BigDecimal.valueOf(1000L), 0, BigDecimal.ROUND_HALF_UP).intValue();
    }


    public static BigDecimal getBetweenHourNumberDecimal(Date start, Date end) {
        Long endTime = end.getTime();
        Long startTime = start.getTime();
        Long betweenTime = endTime - startTime;
        return BigDecimal.valueOf(betweenTime).divide(BigDecimal.valueOf(60 * 60 * 1000L), 2, BigDecimal.ROUND_HALF_UP);
    }


    public static Integer getBetweenDays(Date start, Date end) {
        Long startNum = start.getTime();
        Long endNum = end.getTime();
        if (endNum <= startNum) {
            return 0;
        }
        return ((Long) ((endNum - startNum) / (24 * 60 * 60 * 1000L))).intValue();
    }

    public static String getDistance(Date from, Date to) {
        if (from == null || to == null) return "";
        String re = "";
        Long oneDay = 24 * 60 * 60 * 1000L;
        Long oneHours = 60 * 60 * 1000L;
        Long time = from.getTime() - to.getTime();
        if (time / oneDay != 0) {
            re += ((int) (time / oneDay) + "d");
        }
        time -= ((int) (time / oneDay) * oneDay);
        if (time / oneHours != 0) {
            re += " " + ((int) (time / oneHours) + "h");
        }
        return re;
    }

    public static String getDistanceV2(Date from, Date to, String dayStr, String hoursStr) {
        if (from == null || to == null) return "";
        String re = "";
        Long oneDay = 24 * 60 * 60 * 1000L;
        Long oneHours = 60 * 60 * 1000L;
        Long time = from.getTime() - to.getTime();
        if (time / oneDay != 0) {
            re += ((int) (time / oneDay) + dayStr);
        }
        time -= ((int) (time / oneDay) * oneDay);
        if (time / oneHours != 0) {
            re += " " + ((int) (time / oneHours) + hoursStr);
        }
        return re;
    }

    public static Date getDateToDay(Date date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.parse(format.format(date));
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getFormatDate(Date date, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.parse(format.format(date));
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getDateToDayFirstTime() {
        try {
            Date now = getNow();
            String s = dateToStr(now);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return format.parse((s + " 00:00:00"));
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getDateToDayLastTime() {
        try {
            Date now = getNow();
            String s = dateToStr(now);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return format.parse((s + " 23:59:59"));
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getDate(Object date) {
        if (date instanceof Date) {
            return (Date) date;
        } else if (date instanceof String) {
            return strToDate(date.toString(), "yyyy-MM-dd HH:mm:ss");
        }
        return null;
    }

    public static Date getDate(Object date, String pattern) {
        if (date instanceof Date) {
            return (Date) date;
        } else if (date instanceof String) {
            return strToDate(date.toString(), pattern);
        }
        return null;
    }


    /**
     * 获取两个日期相差的月数
     */
    public static int getMonthDiff(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }

    /**
     * 给定一个时间 减去多少月 返回 yyyy-MM String
     */
    public static String getMonthSubtractNum(Date date,Integer num) {
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.MONTH,num);

        return format(c1.getTime(),"yyyy-MM");
    }

    /**
     * 获取两个时间节点之间的月份列表
     * @param minDate 2018-01
     * @param maxDate 2018-12
     */
    public static List<String> getMonthBetween(String minDate, String maxDate){
        ArrayList<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取两个时间中的每一天
     * @param bigtimeStr 开始时间 yyyy-MM-dd
     * @param endTimeStr 结束时间 yyyy-MM-dd
     */
    public static List<String> getDayBetween(String bigtimeStr, String endTimeStr){
        Date bigtime = null;
        Date endtime = null;
        try {
            bigtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(bigtimeStr + " 00:00:00");
            endtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTimeStr + " 00:00:00");
        } catch (ParseException e) {

        }

        //定义一个接受时间的集合
        List<Date> lDate = new ArrayList<>();
        lDate.add(bigtime);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(bigtime);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(endtime);
        // 测试此日期是否在指定日期之后
        while (endtime.after(calBegin.getTime()))  {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        List<String> datas = new LinkedList<>();
        for (Date date : lDate) {
            datas.add(new SimpleDateFormat("yyyy-MM-dd").format(date));
        }
        return datas;
    }

}
